package com.group15.phms;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Registration3 extends Activity {
	private List<EditText> nameList = new ArrayList<EditText>();
	private List<EditText> emailList = new ArrayList<EditText>();
	private List<EditText> phoneList = new ArrayList<EditText>();
	private ArrayList<String> names = new ArrayList<String>();
	private ArrayList<String> emails = new ArrayList<String>();
	private ArrayList<String> phones = new ArrayList<String>();
	private int count=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registration3);
		
		findViewById(R.id.registerButton).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						register();
					}
				});
		findViewById(R.id.cancelButton).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						cancel();
					}
				});
		findViewById(R.id.addContacts).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						createNew();
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.registration3, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.addbutton:
			createNew();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	//Store values and move to next page
	public void register() {
		EditText e,n,p;
		boolean cancel = false;
		if(nameList.isEmpty()) //if no fields have been added
		{
			setResult(1);
			finish();
		}
		//if one field of a group is filled in, prompt to fill in the missing ones
		//if the last group of fields is empty, ignore
		else
		{
			for (int i=0; i<nameList.size()-1; i++ )
			{
				//System.out.println(nameList.get(i).getText().toString());
				//System.out.println(emailList.get(i).getText().toString());
				//System.out.println(phoneList.get(i).getText().toString());
				
				n=nameList.get(i);
				n.setError(null);
				if (TextUtils.isEmpty(n.toString()))
				{
					n.setError(getString(R.string.error_field_required));
					cancel=true;
				}
				e=emailList.get(i);
				e.setError(null);
				if (TextUtils.isEmpty(e.toString()))
				{
					e.setError(getString(R.string.error_field_required));
					cancel=true;
				}
				p=phoneList.get(i);
				p.setError(null);
				if (TextUtils.isEmpty(p.toString()))
				{
					p.setError(getString(R.string.error_field_required));
					cancel=true;
				}
				
			}
			//test last pair of form fields
			int i=nameList.size()-1;
			n=nameList.get(i);
			e=emailList.get(i);
			p=phoneList.get(i);
			if (TextUtils.isEmpty(n.toString())&&TextUtils.isEmpty(e.toString())&&TextUtils.isEmpty(p.toString()))
				e.setError(null);
			else if (TextUtils.isEmpty(n.toString()))
			{
				n.setError(getString(R.string.error_field_required));
				cancel=true;
			}
			else if (TextUtils.isEmpty(e.toString()))
			{
				e.setError(getString(R.string.error_field_required));
				cancel=true;
			}
			else if (TextUtils.isEmpty(p.toString()))
			{
				p.setError(getString(R.string.error_field_required));
				cancel=true;
			}

			if (cancel) {
				// There was an error; don't attempt login and focus the first form field with an error.
				//focusView.requestFocus();
			} 
			else {
				// go to next page
				for (i=0; i<nameList.size(); i++ )
				{
					names.add(nameList.get(i).getText().toString());
					emails.add(emailList.get(i).getText().toString());
					phones.add(phoneList.get(i).getText().toString());
				}
				Bundle b = new Bundle();
				b.putStringArrayList("names", names);
				b.putStringArrayList("emails", emails);
				b.putStringArrayList("phones", phones);
				Intent result = new Intent();
				result.putExtras(b);
			    
				setResult(2,result);
				finish();
			}
		}
	}
	
	public void cancel() {
		setResult(RESULT_CANCELED);
		finish();
	}
	
	//helper function to set horizontal rule margins
		public static void setMargins (View v, int l, int t, int r, int b) {
		    if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
		        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
		        p.setMargins(l, t, r, b);
		        v.requestLayout();
		    }
		}
		
		//create a new form field
		public void createNew()
		{
			LinearLayout ll = (LinearLayout)findViewById(R.id.mainLayout2);
			int index = ll.indexOfChild(findViewById(R.id.addContacts));
			
			if (count==0)
				index= index-1;
			
			if (count>0)
			{
				View ruler = new View(this);
				LinearLayout.LayoutParams viewLp=new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 10);
				viewLp.setMargins(0,20,0,15);//left,top,right,bottom
				ruler.setLayoutParams(viewLp);
				ruler.setBackgroundColor(Color.parseColor("#0099CC"));
				ll.addView(ruler,index, viewLp);
			}
			
			
			TextView textName = new TextView(this);
	        textName.setText("Name");
	        ll.addView(textName,index+1);
	        
	        EditText nameField=new EditText(this);
	        nameField.setTextColor(Color.BLACK);
	        nameField.setBackgroundResource(R.drawable.edittext_modified_states);
	        nameField.setHint(R.string.first_last);
	        nameField.requestFocus();
	        nameField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
	        try {
		        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
		        mCursorDrawableRes.setAccessible(true);
		        mCursorDrawableRes.set(nameField, 0); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
		    } catch (Exception e) {}
	        ll.addView(nameField,index+2);
	        nameList.add(nameField);
	        
	        TextView textEmail = new TextView(this);
	        textEmail.setText("Email Address");
	        ll.addView(textEmail,index+3);
	        
	        EditText emailField=new EditText(this);
	        emailField.setTextColor(Color.BLACK);
	        emailField.setBackgroundResource(R.drawable.edittext_modified_states);
	        emailField.setHint(R.string.text_email);
	        emailField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
	        try {
		        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
		        mCursorDrawableRes.setAccessible(true);
		        mCursorDrawableRes.set(emailField, 0); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
		    } catch (Exception e) {}
	        ll.addView(emailField,index+4);
	        emailList.add(emailField);
	        
	        TextView textPhone = new TextView(this);
	        textPhone.setText("Phone Number");
	        ll.addView(textPhone,index+5);
	        
	        EditText phoneField=new EditText(this);
	        phoneField.setTextColor(Color.BLACK);
	        phoneField.setBackgroundResource(R.drawable.edittext_modified_states);
	        phoneField.setHint(R.string.phonenum);
	        phoneField.setInputType(InputType.TYPE_CLASS_PHONE);
	        try {
		        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
		        mCursorDrawableRes.setAccessible(true);
		        mCursorDrawableRes.set(phoneField, 0); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
		    } catch (Exception e) {}
	        ll.addView(phoneField,index+6);
	        phoneList.add(phoneField);
	        count++;
		}//end createNew

}
